package com.miniot.base.controller;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.lang.Assert;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.miniot.base.dto.TypeTwoTreeDTO;
import com.miniot.base.entity.TypeTwo;
import com.miniot.base.service.TypeTwoService;
import com.miniot.base.vo.TypeTwoVO;
import com.miniot.business.base.BaseController;
import com.miniot.business.base.R;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.AllArgsConstructor;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 二级类型表
 *
 * @author zzq
 * @date 2023-10-17 11:16:07
 */
@RestController
@AllArgsConstructor
@RequestMapping("/typetwo" )
@Api(value = "二级类型表(后台)", tags = "二级类型表-后台接口")

public class TypeTwoController extends BaseController {

    private final TypeTwoService typeTwoService;

    /**
    * 分页查询
    * @param currentPage 当前页数
    * @param pageSize 每页数据量
    * @param typeTwo 查询条件
    * @return 查询结果
    */
    @GetMapping("/listPage")
    @ApiOperation(value = "分页查询", notes = "传入分页信息与查询条件")
    public R listTypeTwoPage(
            @ApiParam(value = "当前页数", required = true, defaultValue = "1") @RequestParam Long currentPage,
            @ApiParam(value = "每页数据量", required = true, defaultValue = "10") @RequestParam Long pageSize,
            @ApiParam(value = "查询条件") TypeTwo typeTwo) {
        Page<TypeTwo> page = new Page<TypeTwo>(currentPage, pageSize);
        return R.success(typeTwoService.page(page, Wrappers.query(typeTwo).lambda().orderByDesc(TypeTwo::getCreateTime)),"分页信息查询成功");
    }


    /**
    * 通过id查询二级类型表
    * @param id id
    * @return Result
    */
    @GetMapping("/getById")
    @ApiOperation(value = "通过id查询二级类型表", notes = "传入id")
    public R<TypeTwo> getTypeTwoById(@ApiParam(value = "id", required = true) @RequestParam("id") Long id) {
        return R.success(typeTwoService.getById(id));
    }

    /**
     * 通过一级id查询二级类型表
     * @param oneId oneId
     * @return Result
     */
    @GetMapping("/getByOneId")
    @ApiOperation(value = "通过一级id查询二级类型表", notes = "传入id")
    public R<List<TypeTwo>> OneId(@ApiParam(value = "oneId", required = true) @RequestParam("oneId") Long oneId) {
        return R.success(typeTwoService.list(new LambdaQueryWrapper<TypeTwo>().eq(TypeTwo::getTypeOneId,oneId)));
    }

    /**
    * 新增二级类型表
    * @param typeTwo 二级类型表
    * @return Result
    */
    @PostMapping("/save")
    @ApiOperation(value = "新增二级类型表", notes = "传入TypeTwo数据")
    public R<Boolean> save(@RequestBody TypeTwoVO typeTwo) {
        TypeTwo typeTwo1 = new TypeTwo();
        BeanUtil.copyProperties(typeTwo, typeTwo1);
        Assert.isTrue(typeTwoService.save(typeTwo1), "操作失败");
        return R.success(true,"新增成功");
    }

    /**
    * 修改二级类型表
    * @param typeTwo 二级类型表
    * @return Result
    */
    @PostMapping("/updateById")
    @ApiOperation(value = "修改二级类型表", notes = "传入TypeTwo数据")
    public R<Boolean> updateById(@RequestBody TypeTwoVO typeTwo) {
        TypeTwo typeTwo1 = new TypeTwo();
        BeanUtil.copyProperties(typeTwo, typeTwo1);
        Assert.isTrue(typeTwoService.updateById(typeTwo1), "修改失败");
        return R.success(true,"修改成功");
    }

    /**
    * 通过id删除二级类型表
    * @param id id
    * @return Result
    */
    @GetMapping("/deleteById")
    @ApiOperation(value = "通过id删除二级类型表", notes = "传入id")
    public R<Boolean> deleteById(
            @ApiParam(value = "id", required = true) @RequestParam("id") Long id) {
        Assert.isTrue(typeTwoService.removeTypeTwoById(id), "操作失败");
        return R.success(true,"删除成功");
    }
    /**
     * 类型子查询
     * @return Result
     */
    @GetMapping("/getTree")
    @ApiOperation(value = "类型子查询")
    public R<List<TypeTwoTreeDTO>> getTree() {
        return R.success(typeTwoService.getTree(),"删除成功");
    }
}